package demo1;

import java.util.*;

/**
 * 作者： zhi-liao
 * 类的作用:
 * 类的创建时间： 2021/12/5 0:00
 */
public class ArrayTest {
    public static void main(String[] args) {

        int[] array1 = new int[]{1,2,3,9,8};
        int[] array2 = new int[]{2,3,4,5,8,10};
//        setArrayElement(array1);
//        setArrayElement(array2);


//        sort(array1);
//        sort(array2);

//        array1 = deDuplicate(array1);
//        array2 = deDuplicate(array2);

        System.out.println("=========单个数组元素去重后=========");
        foreach(array1);
        System.out.println("===============");
        foreach(array2);
        System.out.println("---------------");
        deDuplicate(array1, array2);
        System.out.println("---------------");

        System.out.println("=========两个个数组元素去重后=========");
        foreach(array1);
        System.out.println("===============");
        foreach(array2);
        System.out.println("---------------");
    }

    static void setArrayElement(int[] array) {
        Random random = new Random();
        for (int i = 0; i < array.length; i++) {
            array[i] = random.nextInt(array.length);
        }
    }

    static void sort(int[] array) {
        Arrays.sort(array);
    }
    static int[] deDuplicate(int[] array1) {
        int n = 0;
        int end = 0;
        int tmp = array1[0];
        for (int i = 1; i < array1.length; i++) {
            if (tmp != array1[i]) {
                tmp = array1[i];
            } else {
                n++;
                for (int j = i-1; j < array1.length-1; j++) {
                    array1[j] = array1[j+1];
                }
                i--;
            }
            if (array1.length == ++end) {
                break;
            }
        }
        return Arrays.copyOfRange(array1, 0, n);
    }
    static void deDuplicate(int[] array1, int[] array2) {
        out: for (int i = 0; i < array1.length; i++) {
            for (int j = 0; j < array2.length; j++) {
                if (array1[i] == array2[j]) {
                    array1[i] = -1;
                    array2[j] = -1;
                    break ;
                }
            }
        }


    }

    static void foreach(int[] array) {
        Arrays.stream(array).forEach((item) -> {
            System.out.print(item + "\t");
        });
        System.out.println();
    }

    //处理数组字符
    private static String[] arrContrast(String[] arr1, String[] arr2) {
        List<String> list = new LinkedList<String>();
        for (String str : arr1) {
            if (!list.contains(str)) {
                list.add(str);
            }
        }

        for (String str : arr2) {
            if (list.contains(str)) {
                list.remove(str);
            }
        }

        for (int i = 0; i < arr1.length; i++) {
            for (int j = 0; j < arr2.length; j++) {
                if (arr1[i].equals(arr2[i])) {
                    arr1[i] = "";
                    arr2[i] = "";
                }
                list.add(arr2[i]);
            }
        }
        Set<String> set = new HashSet<String>();
        for (String string : list) {
            set.add(string);
        }
        list.clear();
        for (String object : set) {
            list.add(object);
        }

        list.remove(0);
//List<String> subList = list.subList(4, 6);
        String[] result = {};
        return list.toArray(result) /*subList.toArray(result)*/;
    }
}
